/** Part of Zombie Navigator
	Copyright © 2016  Zombie Navigator Developers
	
	@file
	@license AGPL-3.0+
	
	Content script on monitor.html
*/ 'use strict';

((w, z, d) => {
	const main = d.getElementById('main');
	main.innerHTML = '';
	d.getElementById('disabled-overlay').remove();
	
	const heading = d.getElementById('heading');
	Array.from(heading.childNodes).forEach(node => node.remove());
	heading.appendChild(d.createTextNode('This window is controlled by a Zombie script'));
	
	const eventList = main.appendChild(d.createElement('ul'));
	z.receive(({type, data, isPrivate}) => {
		const messages = {__proto__: null
			,open: 'Opened'
			,ready: 'Loaded'
			,close: 'Closed'
			,init: 'Starting a script'
			,reload: 'Reloading'
			,screenshot: 'Taking a screenshot'
			,contentScript: 'Injecting a content script'
			,navigate: 'Navigating to a URI'
			,waitForReload: 'Waiting'
		};
		
		const eventItem = d.createElement('li');
		eventItem.classList.add('event-' + type);
		let subject = type in messages ? messages[type] : type;
		if (isPrivate) {
			subject += ' (private)';
			eventItem.classList.add('private');
		}
		
		const date = eventItem.appendChild(d.createElement('code'));
		date.classList.add('date');
		date.appendChild(d.createTextNode(`[${(new Date).toISOString()}]`));
		
		eventItem.appendChild(d.createElement('strong'))
		.appendChild(d.createTextNode(subject));
		
		const dumpProperties = o => {
			const properties = d.createElement('dl');
			Object.getOwnPropertyNames(o).forEach(k => {
				properties.appendChild(d.createElement('dt'))
				.appendChild(d.createTextNode('' + k));
				properties.appendChild(d.createElement('dd'))
				.appendChild('object' == typeof o[k] && null !== o[k]
					? dumpProperties(o[k])
					: (el => (el.appendChild(
							d.createTextNode(JSON.stringify(o[k]))
						), el))(d.createElement('code')));
			});
			return properties;
		};
		
		eventItem.appendChild(dumpProperties(data));
		eventList.appendChild(eventItem);
	});
})(this, this.zombieUI, this.document);
// vim: ts=4 noet ai
